home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Mac / scripts / binhextree.py next >
Encoding:
Text File  |  1996-10-23  |  5.0 KB  |  224 lines  |  [TEXT/Pyth]

  1. #
  2. # binhextree - Recursively descend a directory and
  3. # pack all resource files.
  4. #
  5. # Jack Jansen, CWI, August 1995.
  6. #
  7.  
  8. import os
  9. import binhex
  10. import sys
  11. import macostools
  12. import macfs
  13.  
  14. import aetools
  15. from Metrowerks_Shell_Suite import Metrowerks_Shell_Suite
  16. from Required_Suite import Required_Suite 
  17.  
  18. class MwShell(aetools.TalkTo, Metrowerks_Shell_Suite, Required_Suite):
  19.     pass
  20.  
  21. # Top-level directory
  22. TOP=''
  23.  
  24. # Where to put CW projects, relative to TOP
  25. CWDIR=':Mac:mwerks:projects'
  26. # From which folders to put projects there
  27. CWDIRDIRS=['build.macppc.stand', 'build.macppc.shared', 'build.mac68k.stand', 'build.mac68k.shared', 'PlugIns']
  28.  
  29. # Helper routines
  30. def binhexit(path, name):
  31.     dstfile = path + '.hqx'
  32.     if os.path.exists(dstfile):
  33.         print 'Compare', path,'...',
  34.         if binhexcompare(path, dstfile):
  35.             print 'Identical, skipped.'
  36.             return
  37.         else:
  38.             print 'Not up-to-date.'
  39.     print 'Binhexing', path
  40.     binhex.binhex(path, dstfile)
  41.     
  42. def binhexcompare(source, hqxfile):
  43.     """(source, hqxfile) - Check whether the two files match (forks only)"""
  44.     ifp = binhex.HexBin(hqxfile)
  45.  
  46.     sfp = open(source, 'rb')
  47.     while 1:
  48.         d = ifp.read(128000)
  49.         d2 = sfp.read(128000)
  50.         if d <> d2:
  51.             return 0
  52.         if not d: break
  53.     sfp.close()
  54.     ifp.close_data()
  55.     
  56.     d = ifp.read_rsrc(128000)
  57.     if d:
  58.         sfp = binhex.openrsrc(source, 'rb')
  59.         d2 = sfp.read(128000)
  60.         if d <> d2:
  61.             return 0
  62.         while 1:
  63.             d = ifp.read_rsrc(128000)
  64.             d2 = sfp.read(128000)
  65.             if d <> d2:
  66.                 return 0
  67.             if not d: break
  68.     return 1
  69.  
  70. # Project files to handle
  71. project_files = {}
  72.  
  73. def hexbincwprojects(creator):
  74.     """Compact and hexbin all files remembered with a given creator"""
  75.     try:
  76.         mgr = MwShell(creator, start=1)
  77.     except 'foo':
  78.         print 'Not handled:', creator
  79.         return
  80.     for fss in project_files[creator]:
  81.         srcfile = fss.as_pathname()
  82.         
  83.         if srcfile[-1] == 'µ':
  84.             dstfile = srcfile[:-1]+'mu.hqx'
  85.         elif srcfile[-3] == '.mu':
  86.             dstfile = srcfile + '.hqx'
  87.         elif ord(srcfile[-1]) >= 128:
  88.             dstfile = srcfile[:-1]+`ord(srcfile[-1])`+'.hqx'
  89.         else:
  90.             dstfile = srcfile + '.hqx'
  91.             
  92.         if os.path.exists(dstfile) and \
  93.                 os.stat(dstfile)[8] >= os.stat(srcfile)[8]:
  94.             print 'Skip', dstfile,'- Up-to-date'
  95.             continue
  96.         print 'Compacting', dstfile
  97.         mgr.open(fss)
  98.         mgr.Reset_File_Paths()
  99.         mgr.Remove_Binaries()
  100.         mgr.Close_Project()
  101.         
  102.         print 'Binhexing', dstfile
  103.         binhex.binhex(srcfile, dstfile)
  104.     mgr.quit()
  105.     
  106. def copycwproject(path, name):
  107.     """Copy CW project (if needed) and remember for hexbinning"""
  108.     global project_files
  109.     
  110.     dstdir = os.path.join(TOP, CWDIR)
  111.     if path[:len(dstdir)] == dstdir:
  112.         return
  113.     srcdir = os.path.split(path)[0]
  114.     srcdir = os.path.split(srcdir)[1]
  115.     if srcdir in CWDIRDIRS:
  116.         if not os.path.exists(dstdir):
  117.             print dstdir
  118.             print 'No CW-project dir, skip', name
  119.             return
  120.         dstfile = os.path.join(dstdir, name)
  121.     else:
  122.         if path[-2:] != '.µ':
  123.             return
  124.         dstfile = path[:-2]+ '.mu'
  125.  
  126.     # If the destination doesn't exists or is older that the source
  127.     # we copy and remember it
  128.     
  129.     if os.path.exists(dstfile) and \
  130.             os.stat(dstfile)[8] >= os.stat(path)[8]:
  131.         print 'Not copying', path,'- Up-to-date'
  132.     else:
  133.         print 'Copy', path
  134.         macostools.copy(path, dstfile)
  135.     
  136.     fss = macfs.FSSpec(dstfile)
  137.     creator = fss.GetCreatorType()[0]
  138.     
  139.     if project_files.has_key(creator):
  140.         project_files[creator].append(fss)
  141.     else:
  142.         project_files[creator] = [fss]    
  143.     
  144. def copycwexpfile(path, name):
  145.     """Copy CW export file"""
  146.     global project_files
  147.     
  148.     dstdir = os.path.join(TOP, CWDIR)
  149.     if path[:len(dstdir)] == dstdir:
  150.         return
  151.     srcdir = os.path.split(path)[0]
  152.     srcdir = os.path.split(srcdir)[1]
  153.     if srcdir in CWDIRDIRS:
  154.         if not os.path.exists(dstdir):
  155.             print dstdir
  156.             print 'No CW-project dir, skip', name
  157.             return
  158.         dstfile = os.path.join(dstdir, name)
  159.     else:
  160.         if path[-6:] != '.µ.exp':
  161.             return
  162.         dstfile = path[:-6] + '.mu.exp'
  163.     if dstfile[-6:] == '.µ.exp':
  164.         dstfile = dstfile[:-6]+'.mu.exp'
  165.  
  166.     # If the destination doesn't exists or is older that the source
  167.     # we copy and remember it
  168.     
  169.     if os.path.exists(dstfile) and \
  170.             os.stat(dstfile)[8] >= os.stat(path)[8]:
  171.         print 'Not copying', path,'- Up-to-date'
  172.     else:
  173.         print 'Copy', path
  174.         macostools.copy(path, dstfile)    
  175.  
  176. extensions = [
  177.     ('.rsrc', binhexit),
  178.     ('.gif', binhexit),
  179.     ('.µ', copycwproject),
  180.     ('.µ.exp', copycwexpfile)
  181.     ]
  182.  
  183. def walker(arg, top, names):
  184.     lnames = names[:]
  185.     for n in lnames:
  186.         if n[0] == '(' and n[-1] == ')':
  187.             names.remove(n)
  188.             continue
  189.         for ext, handler in extensions:
  190.             if n[-len(ext):] == ext:
  191.                 name = os.path.join(top, n)
  192.                 handler(name, n)
  193.                 
  194. def dodir(name):
  195.     global TOP, project_files
  196.     TOP = name
  197.     os.path.walk(name, walker, None)
  198.     
  199.     for creator in project_files.keys():
  200.         hexbincwprojects(creator)
  201.     project_files = {}
  202.                 
  203. def main():
  204.     if len(sys.argv) > 1:
  205.         for dir in sys.argv[1:]:
  206.             dodir(dir)
  207.     elif os.name == 'mac':
  208.         import macfs
  209.         dir, ok = macfs.GetDirectory('Folder to search:')
  210.         if not ok:
  211.             sys.exit(0)
  212.         dodir(dir.as_pathname())
  213.     else:
  214.         print 'Usage: hexbintree dir ...'
  215.         sys.exit(1)
  216.     if os.name == 'mac':
  217.         sys.exit(1)   # Keep window
  218.     else:
  219.         sys.exit(0)
  220.         
  221. if __name__ == '__main__':
  222.     main()
  223.     
  224.